package com.tadpole.music.view.activity.me;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.lixs.charts.BarChart.LBarChartView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.me.MoNiErrorV2Adapter;
import com.tadpole.music.bean.me.MoNiErrorBottomBean;
import com.tadpole.music.bean.me.MoNiErrorTopBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.me.MoNiErrorDetailIView;
import com.tadpole.music.presenter.me.MoNiErrorDetailPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.widget.NiceImageView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 模拟练习 错题详情
 * Created By 孙岩
 * on 2020/12/26
 */
public class MoNiErrorDetailActivity extends BaseActivity implements MoNiErrorDetailIView {
    //回退
    private View view_back_icon;
    //头像
    private NiceImageView ivImage;
    //学生姓名
    private TextView tvName;
    //标题
    private TextView tvTitleName;
    //刷新
    private SmartRefreshLayout rl_refresh;
    //做题总数
    private TextView tvOne;
    //做题时长
    private TextView tvTwo;
    //错题总数
    private TextView tvThree;
    //正确率
    private TextView tvFour;
    //柱状图
    private LBarChartView frameNewBase;
    //模拟错题列表
    private RecyclerView recyclerView;
    //adapter
    private MoNiErrorV2Adapter moNiErrorV2Adapter;
    //标题
    private String title;
    //学生id
    private int user_id;
    //等级id
    private int rank_id;
    //分类id
    private int class_id;
    //学生头像
    private String student_avatar;
    //学生昵称
    private String student_nickname;
    //获取上半部分数据
    private MoNiErrorDetailPresenter moNiErrorDetailPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moni_error_detail);
        user_id = getIntent().getIntExtra("user_id", 0);
        rank_id = getIntent().getIntExtra("rank_id", 0);
        class_id = getIntent().getIntExtra("class_id", 0);
        title = getIntent().getStringExtra("title");
        student_avatar = getIntent().getStringExtra("student_avatar");
        student_nickname = getIntent().getStringExtra("student_nickname");
        initViews();
        initListeners();
        initData();

        moNiErrorDetailPresenter = new MoNiErrorDetailPresenter();
        moNiErrorDetailPresenter.attachView(this);
        moNiErrorDetailPresenter.getTop(user_id, rank_id, class_id);
        moNiErrorDetailPresenter.getBottom(user_id, rank_id, class_id);
    }

    //初始化控件
    private void initViews() {
        view_back_icon = findViewById(R.id.view_back_icon);
        ivImage = findViewById(R.id.ivImage);
        tvName = findViewById(R.id.tvName);
        tvTitleName = findViewById(R.id.tvTitleName);
        tvOne = findViewById(R.id.tvOne);
        tvTwo = findViewById(R.id.tvTwo);
        tvThree = findViewById(R.id.tvThree);
        tvFour = findViewById(R.id.tvFour);
        frameNewBase = findViewById(R.id.frameNewBase);
        recyclerView = findViewById(R.id.recyclerView);
        rl_refresh = findViewById(R.id.rl_refresh);
        rl_refresh.setEnableRefresh(false);
    }

    //初始化监听器
    private void initListeners() {
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                MoNiErrorDetailActivity.this.finish();
            }
        });

        rl_refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                moNiErrorDetailPresenter.getBottomMore(user_id, rank_id, class_id);
            }
        });
    }


    //初始化数据
    @SuppressLint("SetTextI18n")
    private void initData() {
        tvName.setText(student_nickname);

        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.mipmap.app_icon)
                .error(R.mipmap.app_icon)
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(this).load(student_avatar).apply(requestOptions).into(ivImage);

        tvTitleName.setText("模拟训练 -" + title + "  错题详情");

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void showMoNiErrorTop(MoNiErrorTopBean.DataBean bean) {
        //总体统计
        tvOne.setText("做题总数" + bean.getTotal_questions() + "题");
        tvTwo.setText("做题总时长" + bean.getTotal_time() + "分钟");
        tvThree.setText("错题总数" + bean.getTotal_err_no() + "道");

        //返回0.58 *100 的精度问题
        BigDecimal bigDecimal = new BigDecimal(bean.getRate());
        String x = bigDecimal.multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString();
        tvFour.setText("正确率" + x + "%");

        //柱形图
        final List<Double> datas = new ArrayList<>();
        final List<String> description = new ArrayList<>();

        datas.add(Double.parseDouble(bean.getTotal_questions() + ""));
        datas.add(Double.parseDouble(bean.getTotal_time() + ""));
        datas.add(Double.parseDouble(bean.getTotal_err_no() + ""));
        String a = ((Double.parseDouble(bean.getRate())) * bean.getTotal_questions()) + "";
        datas.add(Double.parseDouble(a));


        description.add("总题数(道)");
        description.add("总时长(分钟)");
        description.add("错题总数(道)");
        description.add("正确数(道)");


        frameNewBase.setDatas(datas, description, true);
    }

    @Override
    public void showMoNiErrorBottom(final List<MoNiErrorBottomBean.DataBeanX.DataBean> list) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        //创建适配器
        moNiErrorV2Adapter = new MoNiErrorV2Adapter(this, list);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(moNiErrorV2Adapter);
        recyclerView.setNestedScrollingEnabled(false);

        moNiErrorV2Adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if(Mutils.isNetworkAvailable()){
                    if(SpUtil.getInstance(MoNiErrorDetailActivity.this).getString(Constant.TOKEN,"").equals("")){
                        Intent intent = new Intent(MoNiErrorDetailActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }else{

                        if(list.get(position).getErr_num()==0){
                            ToastUtils.show("该学生无错题");
                        }else{
                            Intent intent = new Intent(MoNiErrorDetailActivity.this, StudentErrorActivity.class);
                            intent.putExtra("id",list.get(position).getId());
                            intent.putExtra("flag",1);
                            startActivity(intent);
                        }
                    }
                }else{
                    ToastUtils.show(getResources().getString(R.string.net_work));
                }
            }
        });
    }

    @Override
    public void notifyAdapter() {
        moNiErrorV2Adapter.notifyDataSetChanged();
    }

    @Override
    public void stopRefresh() {
        rl_refresh.finishLoadMore();
    }
}
